package appUtils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Joe on 2017/3/29 0029 15:06.
 * SharedPreferencesManager local data storage
 */

public class SharedPreferencesManager {

    /*constants*/
    public static final String PATTERN_FILE_NAME = "pattern_msg";
    public static final String TOKEN_ID = "token_id";
    public static final String TOKEN_ID_PER = "token_id_per";
    public static final String SECRET_KEY = "secret_key";
    public static final String SECRET_KEY_PER = "secret_key_per";
    public static final String VERIFY_PHONE = "verify";
    public static final String BINDING_LIST = "binding_list";
    public static final String DEFAULT_ACCOUNT_POS = "0";
    public static final String FIRST = "first";
    public static final String ISLIST = "is_list";
    public static final String LOCK_FIRST = "lock_first";
    public static final String TOP_ACTIVITY = "top_activity";

    //check patternLock is ok or not
    public static final String LOCK_FINGERPRINT = "lock_fingerprint";
    public static final String LOCK_GESTURE = "lock_gesture";
    public static final String LOCK_GESTURE_PASS = "lock_gesture_pass";
    public static final String LOCK_DIGITAL = "lock_digital";
    public static final String LOCK_DIGITAL_PASS = "lock_digital_pass";

    /*consants*/
    private static SharedPreferences mSP;
    private static SharedPreferences.Editor mEditor;
    private static SharedPreferencesManager instance;
    private final Context mContext;

    private SharedPreferencesManager(Context context) {
        mContext = context;
        if (null == mSP) {
            mSP = mContext.getSharedPreferences(PATTERN_FILE_NAME, Context.MODE_PRIVATE);
        }
        if (null == mEditor) {
            mEditor = mSP.edit();
        }
    }

    public synchronized static SharedPreferencesManager getInstance(Context context) {
        if (null == instance) {
            synchronized (SharedPreferencesManager.class) {
                if (null == instance) {
                    instance = new SharedPreferencesManager(context);
                }
            }
        }
        return instance;
    }

    public String readString(String key, String defValue) {
        return mSP.getString(key, defValue);
    }

    public void saveString(String key, String value) {
        mEditor.putString(key, value);
        mEditor.apply();
    }

    public int readInt(String key, int defValue) {
        return mSP.getInt(key, defValue);
    }

    public void saveInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.apply();
    }

    public boolean readBoolean(String key, boolean defValue) {
        return mSP.getBoolean(key, defValue);
    }

    public void saveBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.apply();
    }

    public void clean() {
        mEditor.clear();
        mEditor.apply();
    }

}
